/* Includes ------------------------------------------------------------------*/
#include "global.h"
#include <stdio.h>
#include <string.h>
#include "arm_math.h"

/* global variable ---------------------------------------------*/
struct event_queue event_queue = {};

/* private function ---------------------------------------------*/
void loop_start(void);
void loop_end(void);

/*
 * @brief  Main program
 * @param  None
 * @retval None
 */
int main(void)
{
    uint8_t type = 0;
    void *event_payload = 0;

    board_init();
    printf("platform is up\r\n");
    printf("run uart test ...\r\n");

    /* Infinite loop */
    while (1) {
        /* auxiliary things to do before loop*/
        loop_start();

        type = event_fetch(&event_queue, &event_payload);

        if (type == EVENT_NONE) {
            /* count the tick, check if something fails */
        } else if (type == EVENT_IMU_DATA) {
        } else if (type == EVENT_RC_INPUT) {
        } else if (type == EVENT_MAG_DATA) {
        }
        /* auxiliary things to do after loop*/
        loop_end();
    }
}

void loop_start(void)
{
    uint16_t dac_test_data1[10];
    uint16_t dac_test_data2[10];

    static uint64_t start_time = 0;
    uint64_t now = 0;
	static float t = 0.0; 	//1000 * dt / (2 * pi)
	int i;

    now = board_get_systime();
    if (start_time == 0)
        start_time = now;
    if (now - start_time > 1e2) {
        start_time = now;
		for (i = 0; i < 10; i++) {
			dac_test_data1[i] = (uint16_t)(1500.0 * arm_sin_f32(t) + 1500);
			dac_test_data2[i] = (uint16_t)(1500.0 * arm_cos_f32(t) + 1500);
			t += 0.01;
		}
		t -= (10 - board_write_dac(dac_test_data1, dac_test_data2, 10)) * 0.01;
//		board_write_dac(dac_test_data1, dac_test_data2, 10);
    }
}

void loop_end(void)
{
    static uint64_t start_time = 0;
    uint64_t now = 0;

    now = board_get_systime();
    if (start_time == 0)
        start_time = now;
    if (now - start_time > 5e5) {
        start_time = now;
        board_led_toggle(0);
    }
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
